﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Entities;
using ExpertMIS.Brokers;
using System.Collections;
using System.Drawing;

namespace ExpertMIS.Admin.UserControl
{
    public partial class UserAuditForm : System.Web.UI.UserControl
    {
        #region 控件事件处理

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        #endregion

        #region 公有方法


        public ADMUserEntity Entity
        {
            get
            {
                return ViewState["_ADMUserEntity_"] as ADMUserEntity;
            }
            set
            {
                ViewState["_ADMUserEntity_"] = value;
            }
        }

        public void LoadUser(string columnOID)
        {
            ADMUserBroker broker = new ADMUserBroker();

            Hashtable htQuery = new Hashtable();
            htQuery["OID"] = columnOID;
            Entity = broker.GetEntity(htQuery) as ADMUserEntity;

            if (Entity == null)
            {
                Entity = broker.NewEntity() as ADMUserEntity;
            }
            else
            {
                txAccount.Text = Entity.Account;
                //txPassword.Text = "重新设置密码";
                txName.Text = Entity.Name;
                txEmail.Text = Entity.Email;
                rblIsEnable.SelectedValue = Entity.Enable.Value ?"true":"false";
                rblIsPassed.SelectedValue = Entity.IsPassed.Value ? "true" : "false";
                rblIsExpert.SelectedValue = Entity.IsExpert.Value ? "true" : "false";
                rblIsAdmin.SelectedValue = Entity.IsAdmin.Value ? "true" : "false";
            }
        }

        public bool SaveUser()
        {
            try
            {
                ADMUserBroker broker = new ADMUserBroker();

                Entity.Account = txAccount.Text;
                //Entity.Password = txPassword.Text; // HACK，暂未做加密处理
                Entity.Name = txName.Text;
                Entity.Email = txEmail.Text;
                Entity.Enable = bool.Parse(rblIsEnable.SelectedValue);
                Entity.IsPassed = bool.Parse(rblIsPassed.SelectedValue);
                Entity.IsExpert = bool.Parse(rblIsExpert.SelectedValue);
                Entity.IsAdmin = bool.Parse(rblIsAdmin.SelectedValue);

                broker.SaveEntity(Entity, null);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void ResetUserPasword(string OID)
        {
            string newID = "";
            ADMUserBroker broker = new ADMUserBroker();
            broker.ResetPassword(OID, ref newID);
            lblNewPassword.Text = "密码重置为：" + newID;
            lblNewPassword.ForeColor = Color.Red;
            lblNewPassword.Visible = true;
        }

        #endregion
    }
}